<?php

function getWeather($location) {

	$xml = simplexml_load_file('http://www.google.com/ig/api?weather='.utf8_encode($location).'&hl=en');
	$temperature = $xml->xpath("/xml_api_reply/weather/current_conditions/temp_c");
	$wind = $xml->xpath("/xml_api_reply/weather/current_conditions/wind_condition");
	if (!$temperature) {
		return "Don't know location";
	}
	
	$weather = simplexml_load_file("db/weatherConditions.xml");

	foreach($weather->condition as $condition){
		foreach($condition->from as $from){
			if ((integer)$from <= (integer)$temperature[0]->attributes()) {
				if ((integer)$condition->to >= (integer)$temperature[0]->attributes()){
					$testi = $condition->children();
					srand(time());
					$random = ((rand()%count($testi)));
					if ($random < 2) {
						$random = 2;}
					for($i = 2; $i < count($testi); $i++) {
						if ($i == $random){
							$recom = $testi[$i];
						}
					}
				}
			}
		}
	}
	
	$sendOut = "It's " . $temperature[0]->attributes() . " degrees to Celcius outside! </br>". $wind[0]->attributes() . ". </br>" . $recom ;
	return $sendOut;
	
}

function setImage($location) {
	$xml = simplexml_load_file('http://www.google.com/ig/api?weather='.utf8_encode($location).'&hl=en');
	$temperature = $xml->xpath("/xml_api_reply/weather/current_conditions/temp_c");
	
	if (!$temperature) {
		return "";
	}
	
	if ($temperature[0]->attributes() < 0){
		$image = 'images/ilm_volla2.png';
	} else {
		$image = 'images/ilm_volla.png';
	}
	
	return $image;
	
}
?>
